package com.lonefennec.chameleon.ui;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.lonefennec.chameleon.ChameleonGame;

public abstract class TouchButton extends Actor
{
	protected Sprite background;
	protected Sprite sprite;
	
	@Override
	public void draw(SpriteBatch batch, float parentAlpha)
	{
		background.draw(batch);
		sprite.draw(batch);
	}

	@Override
	public Actor hit(float x, float y)
	{
		return sprite.getBoundingRectangle().contains(x, y)? this : null;
	}
	
	protected abstract void onPressDown();
	protected abstract void onPressUp();
	
	@Override
	public boolean touchDown(float x, float y, int pointer)
	{				
		onPressDown();
		
		return true;
	}
	
	@Override
	public void touchUp(float x, float y, int pointer)
	{
		onPressUp();
	}
	
	public TouchButton(String texPath, Vector2 pos, float size)
	{
		String backgroundPath = "ui/hud-button.png";
		
		touchable = true;
		
		ChameleonGame.assets.load(texPath, Texture.class);
		ChameleonGame.assets.load(backgroundPath, Texture.class);
		ChameleonGame.assets.finishLoading();
		
		background = new Sprite(ChameleonGame.assets.get(backgroundPath, Texture.class));
				
		sprite = new Sprite(ChameleonGame.assets.get(texPath, Texture.class));
		sprite.getTexture().setFilter(TextureFilter.Linear, TextureFilter.Linear);
		
		sprite.setBounds(pos.x, pos.y, size, size);
		
		background.setBounds(sprite.getX(), sprite.getY(), sprite.getWidth(), sprite.getHeight());
		background.setColor(new Color(1, 1, 1, 0.75f));
	}
}
